package com.easylinkin.sm.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.easylinkin.sm.entity.SystemConfig;

/**
 * 系统配置缓存类
 * <br>
 *
 * @author OuYangGuoDong
 * @version [版本号, 2015-10-29]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class SystemConfigFacade {

  private SystemConfigFacade() {
  }

  private static Map<String, SystemConfig> configs = new HashMap<>();

  public static void setConfigs(List<SystemConfig> configs) {
    if (null != configs) {
      for (SystemConfig systemConfig : configs) {
        SystemConfigFacade.configs.put(systemConfig.getKey(),
            systemConfig);
      }
    }
  }

  public static SystemConfig get(String key) {
    return SystemConfigFacade.configs.get(key);
  }

  public static List<SystemConfig> getByType(Integer type) {
    List<SystemConfig> list = new ArrayList<>();
    for (SystemConfig config : configs.values()) {
      if (type.equals(config.getType())) {
        list.add(config);
      }
    }
    return list;
  }

  public static String getString(String key) {
    SystemConfig config = get(key);
    if (config != null) {
      return config.getValue();
    }
    return "";
  }

  public static Integer getInteger(String key) {
    String value = getString(key);
    if (value != null && !value.isEmpty()) {
      return Integer.valueOf(value);
    }
    return null;
  }

}
